﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ReName2File
{
    public static class Helper
    {
        #region 辅助方法
        public static string ToLeadingCaps(string name)
        {
            char[] chars = name.ToLower().ToCharArray();
            chars[0] = Char.ToUpper(chars[0]);
            return new string(chars);
        }

        public static string ToLeadingLower(string name)
        {
            char[] chars = name.ToCharArray();
            chars[0] = Char.ToLower(chars[0]);
            return new string(chars);
        }

        public static string ToPascalCase(string name)
        {
            string notStartingAlpha = Regex.Replace(name, "^[^a-zA-Z]+", "");
            string workingString = ToLowerExceptCamelCase(notStartingAlpha);
            workingString = RemoveSeparatorAndCapNext(workingString);
            return workingString;
        }

        public static string RemoveSeparatorAndCapNext(string input)
        {
            string dashUnderscore = "-_";
            string workingString = input;
            char[] chars = workingString.ToCharArray();
            int under = workingString.IndexOfAny(dashUnderscore.ToCharArray());
            while (under > -1)
            {
                chars[under + 1] = Char.ToUpper(chars[under + 1], CultureInfo.InvariantCulture);
                workingString = new String(chars);
                under = workingString.IndexOfAny(dashUnderscore.ToCharArray(), under + 1);
            }
            chars[0] = Char.ToUpper(chars[0], CultureInfo.InvariantCulture);
            workingString = new string(chars);
            return Regex.Replace(workingString, "[-_]", "");
        }

        public static string ToLowerExceptCamelCase(string input)
        {
            char[] chars = input.ToCharArray();
            for (int i = 0; i < chars.Length; i++)
            {
                int left = (i > 0 ? i - 1 : i);
                int right = (i < chars.Length - 1 ? i + 1 : i);
                if (i != left && i != right)
                {
                    if (Char.IsUpper(chars[i]) && Char.IsLetter(chars[left]) && Char.IsUpper(chars[left]))
                    {
                        chars[i] = Char.ToLower(chars[i], CultureInfo.InvariantCulture);
                    }
                    else if (Char.IsUpper(chars[i]) && Char.IsLetter(chars[right]) && Char.IsUpper(chars[right]))
                    {
                        chars[i] = Char.ToLower(chars[i], CultureInfo.InvariantCulture);
                    }
                    else if (Char.IsUpper(chars[i]) && !Char.IsLetter(chars[right]))
                    {
                        chars[i] = Char.ToLower(chars[i], CultureInfo.InvariantCulture);
                    }
                }
            }
            chars[chars.Length - 1] = Char.ToLower(chars[chars.Length - 1], CultureInfo.InvariantCulture);
            return new string(chars);
        }
        #endregion
    }
}
